#include "pch.h"
#include "VAOVBO.h"
#include "applogger.h"



void VAOVBO::init(const GLvoid *pointsPtr, GLsizeiptr  vboSize, std::initializer_list<GLint> attribLengths)
{
    glGenVertexArrays( 1, &this->mVAO );
    glGenBuffers(      1, &this->mVBO );

    // create mVAO
    glBindVertexArray( this->mVAO );

    // create mVBO and transfer buffer data to GPU
    glBindBuffer( GL_ARRAY_BUFFER, this->mVBO );
    glBufferData( GL_ARRAY_BUFFER, 
                vboSize, 
                pointsPtr, 
                GL_STATIC_DRAW );

    
    // calculate stride of the vertex data
    // simply equals to sum of all length of attrib
    GLsizei stride(0);
    for(auto& elem : attribLengths)
        stride += elem;

    // offset of each vertex attrib
    int offset(0);
    //layout (location = i) in vec3 xxx; 
    for(int i=0;i<attribLengths.size();++i)
    {
        glVertexAttribPointer(i, *std::next(attribLengths.begin(), i), GL_FLOAT, GL_FALSE, stride, 
                reinterpret_cast<void*>(offset*sizeof(float)));
        glEnableVertexAttribArray( i );
        offset += *std::next(attribLengths.begin(), i);
    }
     
    //---------------------------------------------//
    //        release bind: VBO, VAO
    //---------------------------------------------//
    // the transferd data can be used when bind VAO again
    glBindBuffer( GL_ARRAY_BUFFER, 0 ); 
    glBindVertexArray( 0 );
}

